package bdd.standalone.bean;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.JoinColumn;

/**
 * Bean Alert.
 * @author Guillaume Pocas & Jean-Fraçois Augé
 */
@Entity
@Table(name = "alert")
public class Alert {

	@Id
	@GeneratedValue
	@Column(name = "alert_id")
	private int alert_id;
	
	@Column(name = "alert_date", nullable = false, updatable = false)
	private Date alert_date;
	
	@Column(name = "alert_source", nullable = false, updatable = false)
	private String alert_source;
	
	@Column(name = "alert_message", nullable = false, updatable = false)
	private String alert_message;
	
	@Column(name = "alert_read", columnDefinition = "Boolean default False")
	private boolean alert_read = false;
	
	@ManyToMany(cascade = {CascadeType.ALL})
    @JoinTable(name="user_alert",
                joinColumns={@JoinColumn(name="user_id")},
                inverseJoinColumns={@JoinColumn(name="alert_id")})
    private Set<User> users = new HashSet<User>();
	
	/**
	 * Créer une nouvelle alerte.
	 * @param alert_date
	 * @param alert_source
	 * @param alert_message
	 */
	public Alert(Date alert_date, String alert_source, String alert_message) {
		this.alert_date = alert_date;
		this.alert_source = alert_source;
		this.alert_message = alert_message;
	}

	/**
	 * Constructeur vide.
	 */
	public Alert() {}

	public int getAlert_id() { return alert_id; }
	public void setAlert_id(int alert_id) { this.alert_id = alert_id; }

	public Date getAlert_date() { return alert_date; }
	public void setAlert_date(Date alert_date) { this.alert_date = alert_date; }

	public String getAlert_source() { return alert_source; }
	public void setAlert_source(String alert_source) { this.alert_source = alert_source; }

	public String getAlert_message() { return alert_message; }
	public void setAlert_message(String alert_message) { this.alert_message = alert_message; }

	public boolean isAlert_read() { return alert_read; }
	public void setAlert_read(boolean alert_read) { this.alert_read = alert_read; }

	public Set<User> getUsers() { return users; }
	public void setUsers(Set<User> users) { this.users = users; }
	
	@Override
	public String toString(){ return getAlert_message(); }
	
}
